//
//  GXGradientView.m
//

#import "GXGradientView.h"
#import "Masonry.h"

@interface GXGradientView ()

@property(nonatomic,weak) UIView *vGradient;

@property (nonatomic, strong) UIPanGestureRecognizer *panGesture;

@property (nonatomic, strong) CABasicAnimation *gradientAnimation;


@property(nonatomic, assign) CGFloat gradientRadius;
@property(nonatomic, assign) CGSize shadowOffsetOrigin;

@property(nonatomic, assign) BOOL isAnimationBegan;

@end

@implementation GXGradientView

//+ (instancetype)gradientViewWithGradientDirection:(GradientDirection)gradientDirection {
//    id obj = [[self alloc] init];
//    ((GXGradientView *)obj).gradientDirection = gradientDirection;
//    return obj;
//}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}


#pragma mark - method
- (void)prepareGradientAnimation {
    if (_isAnimationBegan) {
//        NSLog(@"_isAnimationBegan-----");
        [self.vGradient.layer removeAnimationForKey:@"shadowOffsetAnimation"];
        _isAnimationBegan = NO;
    }
    //
    _vGradient.layer.shadowOffset = _shadowOffsetOrigin; //初始化offset
    _vGradient.alpha = 1; //显示
}

- (void)startGradientAnimation {
//    NSLog(@"isAnimationBegan======: %@", @(self.isAnimationBegan).description);
    if (_isAnimationBegan) {
        return;
    }
    self.isAnimationBegan = YES;
    if (self.gradientTimeInterval == 0) {
        self.gradientTimeInterval = 0.2;
    }
    
//    NSLog(@"_shadowOffsetOrigin.width: %@", @(_shadowOffsetOrigin.width).description);
    _gradientAnimation.duration = _gradientTimeInterval;
    _gradientAnimation.fromValue = [NSValue valueWithCGSize:_vGradient.layer.shadowOffset];
    _gradientAnimation.toValue = [NSValue valueWithCGSize:_shadowOffsetOrigin];
    _gradientAnimation.removedOnCompletion = NO;
    _gradientAnimation.fillMode = kCAFillModeForwards;
    [self.vGradient.layer addAnimation:_gradientAnimation forKey:@"shadowOffsetAnimation"];
    
    [UIView animateWithDuration:self.gradientTimeInterval + 1 animations:^{
        self.vGradient.alpha = 0;
    }];
}




#pragma markl - setupUI
-(void) setupUI {
    self.backgroundColor = [UIColor clearColor];
    self.clipsToBounds = YES;
    
    //vGradient
    UIView *vGradient = [[UIView alloc] init];
    [self addSubview:vGradient];
    self.vGradient = vGradient;
    
    [self.vGradient mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self);
        make.width.mas_offset(2);
    }];
    [self layoutIfNeeded];
    
    self.gradientColor = [UIColor orangeColor];
    self.gradientTimeInterval = 1;
    self.vGradient.backgroundColor = self.gradientColor;
    self.vGradient.alpha = 0;
    
    self.vGradient.layer.shadowRadius = _gradientRadius = 20;
    self.vGradient.layer.shadowOpacity = 1.0;
    
    self.gradientAnimation = [CABasicAnimation animationWithKeyPath:@"shadowOffset.width"]; //实例化动画
}

- (void)initGradient {
    switch (_gradientDirection) {
        case GradientDirectionLeft:
            [self initGradientLeft];
            break;
        case GradientDirectionRight:
            [self initGradientRight];
            break;
            
        default:
            break;
    }
}

- (void)initGradientLeft {
    
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, -5, self.bounds.size.width, self.bounds.size.height + 10)];
    _vGradient.layer.shadowPath = shadowPath.CGPath;
    
    _vGradient.layer.shadowColor = self.gradientColor.CGColor;
    _vGradient.layer.shadowOffset = _shadowOffsetOrigin = (CGSize){-50.0, 0};
    
}

- (void)initGradientRight {
    //remake constraints
    [self.vGradient mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self);
        make.width.mas_offset(2);
    }];
    [self layoutIfNeeded];
    
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, -5, self.bounds.size.width, self.bounds.size.height + 10)];
    _vGradient.layer.shadowPath = shadowPath.CGPath;
    
    _vGradient.layer.shadowColor = self.gradientColor.CGColor;
    _vGradient.layer.shadowOffset = _shadowOffsetOrigin = (CGSize){0.0, 0};
    
}


#pragma mark - setter & getter

- (void)setGradientColor:(UIColor *)gradientColor {
    _gradientColor = gradientColor;
    self.vGradient.backgroundColor = gradientColor;
    self.vGradient.layer.shadowColor = gradientColor.CGColor;
}

- (void)setScale:(CGFloat)scale {
    _scale = scale;
    
    CGFloat offsetWidth = self.vGradient.layer.shadowOffset.width;
    
//    NSLog(@"--scale: %@", @(scale).description);
//    NSLog(@"offsetWidth: %@", @(offsetWidth).description);
//    NSLog(@"self.bounds.size.width: %@", @(self.shadowOffsetOrigin.width).description);
    
    if (self.gradientDirection == GradientDirectionLeft) {
        if (offsetWidth >= 0) {
            self.vGradient.layer.shadowOffset = CGSizeMake(offsetWidth, 0);
        } else {
            self.vGradient.layer.shadowOffset = CGSizeMake(offsetWidth + scale * ABS(self.bounds.size.width), 0);
        }
    } else {
        if (ABS(offsetWidth) >= self.bounds.size.width) {
            self.vGradient.layer.shadowOffset = CGSizeMake(offsetWidth, 0);
        } else {
            self.vGradient.layer.shadowOffset = CGSizeMake(offsetWidth + scale * ABS(self.bounds.size.width), 0);
        }
    }
}

- (void)setGradientDirection:(GradientDirection)gradientDirction {
    _gradientDirection = gradientDirction;
    [self initGradient];
}


@end
